Add TAL-Reverb-II plugin to test
[juce-lv2.git] / juce / source / extras / the jucer / src / ui / jucer_PaintRoutineEditor.cpp
blobc67f6fb8467b7e0918c19111092bb38708206bc4
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../jucer_Headers.h"
27 #include "jucer_PaintRoutineEditor.h"
28 #include "../model/jucer_ObjectTypes.h"
29 #include "jucer_JucerDocumentHolder.h"
32 //==============================================================================
33 PaintRoutineEditor::PaintRoutineEditor (PaintRoutine& graphics_,
34 JucerDocument& document_,
35 JucerDocumentHolder* const docHolder)
36 : graphics (graphics_),
37 document (document_),
38 documentHolder (docHolder),
39 componentOverlay (0),
40 componentOverlayOpacity (0.0f)
42 refreshAllElements();
44 setSize (document.getInitialWidth(),
45 document.getInitialHeight());
48 PaintRoutineEditor::~PaintRoutineEditor()
50 document.removeChangeListener (this);
52 removeAllElementComps();
54 removeChildComponent (&lassoComp);
55 deleteAllChildren();
58 void PaintRoutineEditor::removeAllElementComps()
60 for (int i = getNumChildComponents(); --i >= 0;)
62 PaintElement* const e = dynamic_cast <PaintElement*> (getChildComponent (i));
64 if (e != 0)
65 removeChildComponent (e);
69 const Rectangle<int> PaintRoutineEditor::getComponentArea() const
71 if (document.isFixedSize())
73 return Rectangle<int> ((getWidth() - document.getInitialWidth()) / 2,
74 (getHeight() - document.getInitialHeight()) / 2,
75 document.getInitialWidth(),
76 document.getInitialHeight());
78 else
80 return Rectangle<int> (editorEdgeGap, editorEdgeGap,
81 getWidth() - editorEdgeGap * 2,
82 getHeight() - editorEdgeGap * 2);
86 //==============================================================================
87 void PaintRoutineEditor::paint (Graphics& g)
89 const Rectangle<int> clip (getComponentArea());
91 g.setOrigin (clip.getX(), clip.getY());
92 g.reduceClipRegion (0, 0, clip.getWidth(), clip.getHeight());
94 graphics.fillWithBackground (g, true);
95 grid.draw (g, &graphics);
98 void PaintRoutineEditor::paintOverChildren (Graphics& g)
100 if (componentOverlay.isNull() && document.getComponentOverlayOpacity() > 0.0f)
101 updateComponentOverlay();
103 if (componentOverlay.isValid())
105 const Rectangle<int> clip (getComponentArea());
106 g.drawImageAt (componentOverlay, clip.getX(), clip.getY());
110 void PaintRoutineEditor::resized()
112 if (getWidth() > 0 && getHeight() > 0)
114 componentOverlay = Image();
115 refreshAllElements();
119 void PaintRoutineEditor::updateChildBounds()
121 const Rectangle<int> clip (getComponentArea());
123 for (int i = 0; i < getNumChildComponents(); ++i)
125 PaintElement* const e = dynamic_cast <PaintElement*> (getChildComponent (i));
127 if (e != 0)
128 e->updateBounds (clip);
132 void PaintRoutineEditor::updateComponentOverlay()
134 if (componentOverlay.isValid())
135 repaint();
137 componentOverlay = Image();
138 componentOverlayOpacity = document.getComponentOverlayOpacity();
140 if (componentOverlayOpacity > 0.0f)
142 if (documentHolder != 0)
143 componentOverlay = documentHolder->createComponentLayerSnapshot();
145 if (componentOverlay.isValid())
147 componentOverlay.multiplyAllAlphas (componentOverlayOpacity);
148 repaint();
153 void PaintRoutineEditor::visibilityChanged()
155 document.getUndoManager().beginNewTransaction();
157 if (isVisible())
159 refreshAllElements();
160 document.addChangeListener (this);
162 else
164 document.removeChangeListener (this);
165 componentOverlay = Image();
169 void PaintRoutineEditor::refreshAllElements()
171 int i;
172 for (i = getNumChildComponents(); --i >= 0;)
174 PaintElement* const e = dynamic_cast <PaintElement*> (getChildComponent (i));
176 if (e != 0 && ! graphics.containsElement (e))
177 removeChildComponent (e);
180 Component* last = 0;
182 for (i = graphics.getNumElements(); --i >= 0;)
184 PaintElement* const e = graphics.getElement (i);
186 addAndMakeVisible (e);
188 if (last != 0)
189 e->toBehind (last);
190 else
191 e->toFront (false);
193 last = e;
196 updateChildBounds();
198 if (grid.updateFromDesign (document))
199 repaint();
201 if (currentBackgroundColour != graphics.getBackgroundColour())
203 currentBackgroundColour = graphics.getBackgroundColour();
204 grid.updateColour();
205 repaint();
208 if (componentOverlayOpacity != document.getComponentOverlayOpacity())
210 componentOverlay = Image();
211 componentOverlayOpacity = document.getComponentOverlayOpacity();
212 repaint();
216 void PaintRoutineEditor::changeListenerCallback (ChangeBroadcaster* source)
218 refreshAllElements();
221 void PaintRoutineEditor::mouseDown (const MouseEvent& e)
223 if (e.mods.isPopupMenu())
225 PopupMenu m;
227 m.addCommandItem (commandManager, CommandIDs::editCompLayout);
228 m.addCommandItem (commandManager, CommandIDs::editCompGraphics);
229 m.addSeparator();
231 for (int i = 0; i < ObjectTypes::numElementTypes; ++i)
232 m.addCommandItem (commandManager, CommandIDs::newElementBase + i);
234 m.show();
236 else
238 addChildComponent (&lassoComp);
239 lassoComp.beginLasso (e, this);
243 void PaintRoutineEditor::mouseDrag (const MouseEvent& e)
245 lassoComp.toFront (false);
246 lassoComp.dragLasso (e);
249 void PaintRoutineEditor::mouseUp (const MouseEvent& e)
251 lassoComp.endLasso();
253 if (e.mouseWasClicked() && ! e.mods.isAnyModifierKeyDown())
255 graphics.getSelectedElements().deselectAll();
256 graphics.getSelectedPoints().deselectAll();
260 void PaintRoutineEditor::findLassoItemsInArea (Array <PaintElement*>& results, const Rectangle<int>& lasso)
262 for (int i = 0; i < getNumChildComponents(); ++i)
264 PaintElement* const e = dynamic_cast <PaintElement*> (getChildComponent (i));
266 if (e != 0 && e->getBounds().expanded (-e->borderThickness, -e->borderThickness)
267 .intersects (lasso))
268 results.add (e);
272 SelectedItemSet <PaintElement*>& PaintRoutineEditor::getLassoSelection()
274 return graphics.getSelectedElements();
277 bool PaintRoutineEditor::isInterestedInFileDrag (const StringArray& files)
279 const File f (files [0]);
281 return f.hasFileExtension ("jpg")
282 || f.hasFileExtension ("jpeg")
283 || f.hasFileExtension ("png")
284 || f.hasFileExtension ("gif")
285 || f.hasFileExtension ("svg");
288 void PaintRoutineEditor::filesDropped (const StringArray& filenames, int x, int y)
290 const File f (filenames [0]);
292 if (f.existsAsFile())
294 Drawable* d = Drawable::createFromImageFile (f);
296 if (d != 0)
298 delete d;
300 document.getUndoManager().beginNewTransaction();
302 graphics.dropImageAt (f,
303 jlimit (10, getWidth() - 10, x),
304 jlimit (10, getHeight() - 10, y));
306 document.getUndoManager().beginNewTransaction();